<template>
  <div>
    <el-card class="box-card">
      <img src="../assets/images/111.jpg" alt="">
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-radio v-model="loginForm.type" label="1">学生</el-radio>
          <el-radio v-model="loginForm.type" label="0">管理员</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="register" id="register">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        type: '1'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
      },
    };
  },
  methods: {
    async login () {
      const res = await this.$http.post('api/login', this.loginForm)
      if (res.status !== 200) return this.$message.error('登录失败,请检查用户名或密码！')
      this.$store.state.token = res.token
      this.$store.state.userInfo = res.userInfo
      // console.log(this.$store.state.userInfo)
      // console.log('1111', this.$store.token)
      // window.sessionStorage.setItem('token', res.token)
      this.$message.success(`登录成功，欢迎您:${res.userInfo.userName}`)
      this.$router.push('/home')
    },
    register () {
      this.$message.warning('不允许注册,请联系管理员新建账号!')
    }
  },
  mounted () { },
  computed: {},
  watch: {},
};
</script>

<style scoped lang="less">
.el-card {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 40%;
  height: 400px;
  overflow: visible;
  img {
    width: 100px;
    border-radius: 50%;
    position: absolute;
    top: -50px;
    left: calc(50% - 50px);
  }
  .el-form {
    margin-top: 100px;
    .el-form-item {
      width: 60%;
      margin-left: 80px;
    }
  }
}
#register {
  margin-left: 60px;
}
</style>
